﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace Chaotic.Core.Extension
{
    public static  class ApplicationExtension
    {
        public static void BeginInvokeOnUIThread(Action action)
        {
            Dispatcher dispatcher = ((Application.Current != null && Application.Current.Dispatcher != null) ? 
                Application.Current.Dispatcher : 
                Dispatcher.CurrentDispatcher);
            if (dispatcher.CheckAccess() && dispatcher.Thread.GetApartmentState() == ApartmentState.STA)
            {
                action();
            }
            else
            {
                dispatcher.BeginInvoke(action);
            }
        }

        public static void InvokeOnUIThread(Action action)
        {
            Dispatcher dispatcher = ((Application.Current != null && Application.Current.Dispatcher != null) ?
                Application.Current.Dispatcher :
                Dispatcher.CurrentDispatcher);
            if (dispatcher.CheckAccess() && dispatcher.Thread.GetApartmentState() == ApartmentState.STA)
            {
                action();
            }
            else
            {
                dispatcher.Invoke(action);
            }
        }
    }
}
